
var entityNum ="";
$(function () {
    "use strict";
    loadProduct();
    getOnlyMyInfo();
    //导出
    $('#export').click(function () {
        exportProductInfo();
    });
});
function entitySelect(){
    //获取被选中的option标签
     var vs = $('select option:selected').val();
     entityNum = vs;
     loadProduct();
}
function getOnlyMyInfo(){
    $.ajax({
            type: "get",
            url: "/auth/user/me/",
           // data: JSON.stringify(params),
            contentType: "application/json",
            success: function (result) {
                   var myData = result;
                   if(myData.joinUserOrg.length>0){
                        $("#entity").append($.templates("#entity-list-tmpl").render(myData.joinUserOrg));
                   }else{
//                        var datsss = [{"clientId":"111","orgName":"heihei111"},{"clientId":"222","orgName":"heihei222"}];
//                        $("#entity").append($.templates("#entity-list-tmpl").render(datsss));
                   }
            }
        });
}
var indexPa=1
var totalSum=0;
var productname="";
//http://cloud.vimi8.top/industrial/product/allProductInfo?index=1&size=10&productName=yy&farmName=%E9%93%9C%E4%BB%81
function loadProduct() {
    $.ajax({
        type: "get",
        url: "/ebb/entity/entityproductinfo/?start="+indexPa+"&size=10&productName="+productname+"&entityId="+entityNum,
       // data: JSON.stringify(params),
        contentType: "application/json",
        success: function (result) {
            if (result.data.length > 0) {
                var totalCount = result.totalPages;
                totalPage = totalCount;
                $("#total-page").text(totalPage);

                $("#pagination-bar").show();
                $("#pagination-box").html('<ul id="pagination" class="pagination"></ul>');
                $('#pagination').twbsPagination({
                    totalPages: totalCount,
                    visiblePages: 5,
                    first: '第一页',
                    prev: '上一页',
                    next: '下一页',
                    last: '最后页',
                    onPageClick: function(event,page) {
                        indexPa=page;

                        $.ajax({
                                type: "get",
                                url: "/ebb/entity/entityproductinfo/?start="+indexPa+"&size=10&productName="+productname+"&entityId="+entityNum,
                             //   data: JSON.stringify(params),
                                contentType: "application/json",
                                success: function (result) {
                                    totalSum = result.totalPages;
                                    var d = result.data;
                                    //渲染数据
                                    $.templates("#product-list-tmpl").link("#product-list-data", d);
                                    totalPages = result.totalPages;
                                    $('#product-count').text(result.totalCount);
                                    $('#page-total').text(result.totalPages);
                                    $('#page-index').val(page);
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    if(XMLHttpRequest.status==401){
                                                                                    //跳转到登录页,带上登录后的跳转地址redirect_uri=???
                                                                                    // redirect_login();

                                                                                     ShowTip("你还没有登录，请先登录！", "success", 1000);
                                                                                                                            setTimeout(function () {
                                                                                                                                window.location = "login.html";
                                                                                                                            }, 1000);
                                                                                }
                                }
                            });
                    },
                    startPage: indexPa
                });
            } else {
                $("#product-list-data").html("<tr><td colspan='8'><h1 align='center' height='300px'>暂无数据</h1></td></tr>");
                $("#pagination-bar").hide();
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
               if(XMLHttpRequest.status==401){
                                 //跳转到登录页,带上登录后的跳转地址redirect_uri=???
                                 // redirect_login();
                  ShowTip("你还没有登录，请先登录！", "success", 1000);
                 setTimeout(function () {
                     window.location = "login.html";
                 }, 1000);
            }

         }
    });
}

function comments() {
  productname= $("#productName").val().trim();
  loadProduct();
}




$('#go-index').click(function () {
    var index = $('#page-index').val();
    if(index>totalSum){
        index = totalSum;
    }
  indexPa=parseInt(index);
    loadProduct();
});



//删掉专产品http://cloud.vimi8.top/industrial/product/?id=old_5606f0798200756ca8cc621d
function deleteProduct(id){
     if(confirm("确定要删除该产品吗？")){
      $.ajax({
       type:"DELETE",
       url:"/ebb/entity/product/?id="+id,
        dataType: "json",
        success: function(data){
            if(!data.error){
              window.location.reload();
            }
        }
    });
   }
}

//导出产品信息
function exportProductInfo() {
    var xiProduct = $("#productName").val();
    window.location.href = "/ebb/entity/product/export?productName=" + xiProduct;
}












